package com.copyctrl.campus.portal.service.Impl;

import com.copyctrl.campus.portal.service.GoodsService;
import com.copyctrl.mapper.CampGoodsMapper;
import com.copyctrl.model.CampGoods;
import com.copyctrl.model.CampGoodsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:JH
 * @version:1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    CampGoodsMapper mapper;

    @Override
    public int add(CampGoods goods) {
        goods.setTime(new Date());
        return mapper.insert(goods);
    }

    @Override
    public int update(CampGoods goods) {
        CampGoodsExample example=new CampGoodsExample();
        example.createCriteria().andIdEqualTo(goods.getId());
        return  mapper.updateByExampleSelective(goods,example);
    }

    @Override
    public int delete(CampGoods goods) {
        return mapper.deleteByPrimaryKey(goods.getId());
    }

    @Override
    public Map<String,List<CampGoods>> findAll(Integer id) {
        CampGoodsExample example = new CampGoodsExample();
        example.createCriteria().andIdIsNotNull();
        List<CampGoods> campGoods = mapper.selectByExample(example);
        Map<String,List<CampGoods>> result = new HashMap<>();
        result.put("all",campGoods);
        example.clear();
        example.createCriteria().andUidEqualTo(id);
        List<CampGoods> myGoods = mapper.selectByExample(example);
        result.put("mine",myGoods);
        example.clear();
        example.createCriteria().andIdIsNotNull().andNumberGreaterThan(0);
        List<CampGoods> notSale = mapper.selectByExample(example);
        result.put("onsale",notSale);
        return result;
    }

    @Override
    public List<CampGoods> findByUser(Integer id) {
        CampGoodsExample example = new CampGoodsExample();
        example.createCriteria().andUidEqualTo(id);
        return mapper.selectByExample(example);
    }
}
